package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.mian.data.MainDataDeductionRulesDTO;
import com.ctshk.rpc.system.dto.mian.data.MainDataDeductionRulesDetailsDTO;
import com.ctshk.rpc.system.dto.mian.data.MainDataNotGroupRulesDTO;
import com.ctshk.rpc.system.entity.*;
import com.ctshk.rpc.system.mapper.MainDataDeductionRulesDetailsMapper;
import com.ctshk.rpc.system.mapper.MainDataDeductionRulesMapper;
import com.ctshk.rpc.system.req.main.data.*;
import com.ctshk.rpc.system.service.IMainDataDeductionRulesService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 退团扣费规则 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-01-26
 */
@DubboService
public class MainDataDeductionRulesServiceImpl extends ServiceImpl<MainDataDeductionRulesMapper, MainDataDeductionRules> implements IMainDataDeductionRulesService {

    @Autowired
    private MainDataDeductionRulesDetailsMapper mainDataDeductionRulesDetailsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(MainDataDeductionRulesReq req, TokenUser tokenUser) {
        MainDataDeductionRules mainDataDeductionRules = EntityUtil.copy(req, MainDataDeductionRules.class);
        long id = IdWorker.getId();
        LocalDateTime now = LocalDateTime.now();
        mainDataDeductionRules.setId(id);
        mainDataDeductionRules.setCreateId(tokenUser.getId());
        mainDataDeductionRules.setGmtCreate(now);
        mainDataDeductionRules.setModifiedId(tokenUser.getId());
        mainDataDeductionRules.setGmtModified(now);
        mainDataDeductionRules.setIsDeleted(IsDeletedCode.NO.getCode());
        baseMapper.insert(mainDataDeductionRules);
        List<MainDataDeductionRulesDetailsReq> rulesDetailsReqList = req.getRulesDetailsReqList();
        for (MainDataDeductionRulesDetailsReq mainDataDeductionRulesDetailsReq:req.getRulesDetailsReqList()) {
            MainDataDeductionRulesDetails rulesDetails = EntityUtil.copy(mainDataDeductionRulesDetailsReq, MainDataDeductionRulesDetails.class);
            rulesDetails.setGroupId(id);
            rulesDetails.setGmtCreate(now);
            rulesDetails.setCreateId(tokenUser.getId());
            mainDataDeductionRulesDetailsMapper.insert(rulesDetails);
        }
        return Result.success(id);
    }

    @Override
    public PageResponse<MainDataDeductionRulesDTO> queryList(MainDataDeductionRulesQueryReq req) {
        IPage<MainDataDeductionRules> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<MainDataDeductionRules> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(null != req.getId(),MainDataDeductionRules::getId,req.getId());
        queryWrapper.lambda().eq(MainDataDeductionRules::getIsDeleted, 0).orderByDesc(MainDataDeductionRules::getGmtModified);
        if (null != req.getStatus()) {
            queryWrapper.lambda().eq(MainDataDeductionRules::getStatus, req.getStatus());
        }
        if (StringUtils.isNotBlank(req.getDeductionRules())) {
            queryWrapper.lambda().like(MainDataDeductionRules::getDeductionRules, req.getDeductionRules());
        }
        queryWrapper.lambda().orderByDesc(MainDataDeductionRules::getGmtModified);
        IPage<MainDataDeductionRules> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataDeductionRules> mainDataDeductionRulesList = iPage.getRecords();
        if(CollectionUtils.isEmpty(mainDataDeductionRulesList)){
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataDeductionRulesDTO> mainDataDeductionRulesDTOList = EntityUtil.copyList(mainDataDeductionRulesList, MainDataDeductionRulesDTO.class);
        for (MainDataDeductionRulesDTO mainDataDeductionRulesDTO:mainDataDeductionRulesDTOList) {
            QueryWrapper<MainDataDeductionRulesDetails> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(MainDataDeductionRulesDetails::getGroupId,mainDataDeductionRulesDTO.getId());
            wrapper.lambda().orderByAsc(MainDataDeductionRulesDetails::getBeforeDepartureDays);
            List<MainDataDeductionRulesDetails> rulesDetails = mainDataDeductionRulesDetailsMapper.selectList(wrapper);
            List<MainDataDeductionRulesDetailsDTO> detailsList = EntityUtil.copyList(rulesDetails, MainDataDeductionRulesDetailsDTO.class);
            mainDataDeductionRulesDTO.setRulesDetailsDTOList(detailsList);
        }
        return new PageResponse<>(mainDataDeductionRulesDTOList, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(MainDataDeductionRulesIdReq req, TokenUser tokenUser) {
        MainDataDeductionRules mainDataDeductionRules = new MainDataDeductionRules();
        mainDataDeductionRules.setModifiedId(tokenUser.getId());
        mainDataDeductionRules.setGmtModified(LocalDateTime.now());
        mainDataDeductionRules.setId(req.getId());
        mainDataDeductionRules.setIsDeleted(1);
        baseMapper.updateById(mainDataDeductionRules);
        return Result.success(req.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(MainDataDeductionRulesUpdateReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        QueryWrapper<MainDataDeductionRulesDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataDeductionRulesDetails::getGroupId,req.getId());
        mainDataDeductionRulesDetailsMapper.delete(queryWrapper);
        for (MainDataDeductionRulesDetailsReq details:req.getRulesDetailsReqList()) {
            MainDataDeductionRulesDetails rulesDetails = EntityUtil.copy(details, MainDataDeductionRulesDetails.class);
            rulesDetails.setGmtCreate(now);
            rulesDetails.setGmtModified(now);
            rulesDetails.setCreateId(tokenUser.getId());
            mainDataDeductionRulesDetailsMapper.insert(rulesDetails);
        }
        MainDataDeductionRules mainDataDeductionRules = EntityUtil.copy(req, MainDataDeductionRules.class);
        mainDataDeductionRules.setModifiedId(tokenUser.getId());
        mainDataDeductionRules.setGmtModified(now);
        baseMapper.updateById(mainDataDeductionRules);
        return Result.success(req.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateStatus(MainDataDeductionRulesIdReq req, TokenUser tokenUser) {
        MainDataDeductionRules mainDataDeductionRules = baseMapper.selectById(req.getId());
        MainDataDeductionRules deductionRules = new MainDataDeductionRules();
        deductionRules.setGmtModified(LocalDateTime.now());
        deductionRules.setModifiedId(tokenUser.getId());
        if (mainDataDeductionRules.getStatus() == 0) {
            deductionRules.setId(req.getId());
            deductionRules.setStatus(1);
        }
        if (mainDataDeductionRules.getStatus() == 1) {
            deductionRules.setId(req.getId());
            deductionRules.setStatus(0);
        }
        baseMapper.updateById(deductionRules);
        return Result.success(req.getId());
    }

    @Override
    public MainDataDeductionRulesDTO query(MainDataDeductionRulesIdReq req) {
        MainDataDeductionRules mainDataDeductionRules = baseMapper.selectById(req.getId());
        MainDataDeductionRulesDTO mainDataDeductionRulesDTO = EntityUtil.copy(mainDataDeductionRules, MainDataDeductionRulesDTO.class);
        QueryWrapper<MainDataDeductionRulesDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MainDataDeductionRulesDetails::getGroupId,mainDataDeductionRules.getId());
        queryWrapper.lambda().orderByAsc(MainDataDeductionRulesDetails::getBeforeDepartureDays);
        List<MainDataDeductionRulesDetails> mainDataDeductionRulesDetails = mainDataDeductionRulesDetailsMapper.selectList(queryWrapper);
        List<MainDataDeductionRulesDetailsDTO> dtos = EntityUtil.copyList(mainDataDeductionRulesDetails, MainDataDeductionRulesDetailsDTO.class);
        mainDataDeductionRulesDTO.setRulesDetailsDTOList(dtos);
        return mainDataDeductionRulesDTO;
    }
}
